/*
 * Created by JFormDesigner on Thu Jun 27 08:35:34 CST 2024
 */

package liyujie.ui;

import java.awt.event.*;
import javax.swing.event.*;
import liyujie.dao.UserDao;
import liyujie.model.User;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author lenovo
 */
public class panelUsermanager extends JPanel {
    DefaultTableModel model;
    FormMain main;
    public panelUsermanager(FormMain main) {
        this.main=main;
        initComponents();
        initView();
    }
    private void initView() {
        model=(DefaultTableModel) tabUser.getModel();
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("密码");
        model.addColumn("性别");
        model.addColumn("年龄");
        model.addColumn("电话号码");
        getTable();
    }
    private void getTable(){
        model.setRowCount(1);
        UserDao userDao=new UserDao();
        List<User> allUser=userDao.getAllUser();

        for(User user:allUser){
            model.addRow(new Object[]{user.getId(),user.getUsername(),user.getPassword(),
                    user.getGender(),user.getAge(),user.getMobile()});
        }
    }



    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgUserAdd dlgUserAdd = new DlgUserAdd(main);
        dlgUserAdd.setModal(true);
        dlgUserAdd.setVisible(true);
    }

    private void tabUserAncestorAdded(AncestorEvent e) {
        // TODO add your code here
    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
       if (tabUser.getSelectedRowCount()==0){
           JOptionPane.showMessageDialog(this,"请输入要修改的用户");
           return;
       }
       int index=tabUser.getSelectedRow();
       int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgUsserUpdate dlgUsserUpdate = new DlgUsserUpdate(main, id);
        dlgUsserUpdate.setModal(true);
        dlgUsserUpdate.setVisible(true);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - LILI
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax .
        swing. border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border
        . TitledBorder. CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog"
        , java .awt . Font. BOLD ,12 ) ,java . awt. Color .red ) , getBorder
        () ) );  addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java
        . beans. PropertyChangeEvent e) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException
        ( ) ;} } );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {

            //---- tabUser ----
            tabUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            tabUser.addAncestorListener(new AncestorListener() {
                @Override
                public void ancestorAdded(AncestorEvent e) {
                    tabUserAncestorAdded(e);
                }
                @Override
                public void ancestorMoved(AncestorEvent e) {}
                @Override
                public void ancestorRemoved(AncestorEvent e) {}
            });
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - LILI
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
